package com.hxalex.simulationhandle.common;

import android.app.Activity;
import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hxalex.simulationhandle.util.GlideRoundTransform;
import com.hxalex.simulationhandle.util.GlideRoundTransformv2;


/**
 * Created by qiuda on 16/6/13.
 */
public class MVVPSetters {

    @BindingAdapter({"image", "defaultImage"})
    public static void imageLoader(ImageView view, String url, Drawable defaultImage) {
        Glide.with(view.getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).error
                (defaultImage).into(view);
    }

    @BindingAdapter({"image", "aspect"})
    public static void imageLoader(ImageView view, String url, float aspect) {
        Glide.with(view.getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(view.getContext(), (int) aspect)).centerCrop()
                .into(view);
    }

    @BindingAdapter({"image", "aspect", "dontAnimate"})
    public static void imageLoader(ImageView view, String url, float aspect, boolean dontAnimate) {
        if (dontAnimate) {
            Glide.with(view.getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL)
                    .dontAnimate().centerCrop().transform(new GlideRoundTransform(view.getContext
                    (), (int) aspect)).into(view);
        } else {
            Glide.with(view.getContext()).load(url).centerCrop().diskCacheStrategy
                    (DiskCacheStrategy.ALL).transform(new GlideRoundTransform(view.getContext(),
                    (int) aspect)).into(view);
        }
    }

    @BindingAdapter({"image", "errorImage", "aspect", "dontAnimate"})
    public static void imageLoader(ImageView view, String url, Drawable errorImage, float aspect,
                                   boolean dontAnimate) {
        if (view.getContext() instanceof Activity) {
            Activity activity = (Activity) view.getContext();
            if (activity.isDestroyed() || activity.isFinishing()) {
                return;
            }
        }
        if (dontAnimate) {
            Glide.with(view.getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL)
                    .dontAnimate().centerCrop().transform(new GlideRoundTransform(view.getContext
                    (), (int) aspect)).error(errorImage).into(view);
        } else {
            Glide.with(view.getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop().transform(new GlideRoundTransform(view.getContext(), (int)
                    aspect)).error(errorImage).into(view);
        }
    }

    @BindingAdapter({"image", "defaultImage", "aspect"})
    public static void imageLoader(ImageView view, String url, Drawable defaultImage, float
            aspect) {
        try {
            Glide.with(view.getContext()).load(url).error(defaultImage).diskCacheStrategy
                    (DiskCacheStrategy.ALL).
                    centerCrop().transform(new GlideRoundTransform(view.getContext(), (int)
                    aspect)).into(view);
        } catch (IllegalArgumentException e) {

        }
    }

    @BindingAdapter({"image", "radius"})
    public static void roundImageLoader(ImageView view, String url, int radius) {
        Glide.with(view.getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransformv2(view.getContext(), radius)).into(view);
    }

    @BindingAdapter({"image", "radius", "defaultImage"})
    public static void roundImageLoader(ImageView view, String url, int radius, Drawable
            defaultImage) {
        Glide.with(view.getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).error
                (defaultImage).transform(new GlideRoundTransformv2(view.getContext(), radius))
                .into(view);
    }

 

    @BindingAdapter({"image"})
    public static void imageLoader(ImageView view, String url) {
        Glide.with(view.getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(view);
    }


}

